package cp3120.view;

import cp3120.model.Credentials;
import cp3120.model.Password;
import cp3120.net.ActiveUser;
import cp3120.ucm.client.*;
import cp3120.ucm.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * The Main menu for the Flight Manager application.
 *
 * The menu is modal, that is depending on who is signed in different menus are displayed.
 * This requires that the application updates its mode by calling setMode whenever the mode is changed.
 *
 * @author Adam Scarr
 * @since r7
 */
public class MainMenu extends JMenuBar {
    private JMenuItem loginUser;
    private JMenuItem registerUser;
    private JMenuItem updateDetails;
    private JMenuItem deleteUser;
    private JMenuItem exit;
    private JMenuItem about;
    private JMenuItem createFlight;
    private JMenuItem airports;
    private JMenuItem roles;
    private JMenuItem users;
    private JMenuItem activeUsers;
    private JMenuItem searchFlight;
    private JMenuItem promotions;
    private JMenuItem tax;
    private JMenu file;
    private JMenu help;
    private JMenu staff;

    public static final int USER = 1;

    /**
     * Rebuilds the menu depending on the current users permissions.
     */
    public void rebuildMenu() {
        removeAll();
        System.out.println("[Menu] Rebuilding menu for user: " + ActiveUser.getUser());

        file.removeAll();
        file.add(loginUser);

        if (ActiveUser.hasPermission("Search Flight")) {
            file.add(searchFlight);
        }

        if (ActiveUser.hasPermission("Register Account")) file.add(registerUser);
        if (ActiveUser.hasPermission("Manage Own Account")) {
            file.add(updateDetails);
            file.add(deleteUser);
        }

        file.add(new JSeparator());
        file.add(exit);

        add(file);

        staff.removeAll();

        if (ActiveUser.hasPermission("Manage Flights")) staff.add(createFlight);
        if (ActiveUser.hasPermission("Manage Airports")) staff.add(airports);
        if (ActiveUser.hasPermission("Manage Roles")) staff.add(roles);
        if (ActiveUser.hasPermission("Manage Users")) staff.add(users);
        if (ActiveUser.hasPermission("View Active Users")) staff.add(activeUsers);
        if (ActiveUser.hasPermission("Manage Tax")) staff.add(tax);
        if (ActiveUser.hasPermission("Manage Promotions")) staff.add(promotions);


        if (staff.getMenuComponentCount() > 0) {
            add(staff);
        }

        help.removeAll();
        add(help);
        help.add(about);

        revalidate();
    }

    /**
     * Class Constructor that sets up all internal components.
     */
    public MainMenu() {
        file = new JMenu("File");
        file.setMnemonic('f');

        loginUser = new JMenuItem("Login");
        loginUser.setMnemonic('u');
        loginUser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                MainUI.getInstance().getLoginDialogue().setVisible(true);
                MainUI.getInstance().getLoginDialogue().setLoginListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        MainUI.getInstance().loginUser(new Credentials(RealMainUI.getInstance().getLoginDialogue().getUsername(), new Password(RealMainUI.getInstance().getLoginDialogue().getPassword())));
                    }
                });
            }

        });

        searchFlight = new JMenuItem("Find Flights");
        searchFlight.setMnemonic('f');
        searchFlight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainUI.getInstance().getFlightSearchbox().setVisible(true);
            }
        });

        registerUser = new JMenuItem("Register new User");
        registerUser.setMnemonic('n');
        registerUser.addActionListener(new UC2ALAdapter(new UCDisplayRegistration()));

        updateDetails = new JMenuItem("Update Details");
        updateDetails.setMnemonic('n');
        updateDetails.addActionListener(new UC2ALAdapter(new UCDisplayUpdateDetails()));

        deleteUser = new JMenuItem("Delete User");
        deleteUser.setMnemonic('d');
        deleteUser.addActionListener(new UC2ALAdapter(new UCDisplayConfirmDelete()));

        exit = new JMenuItem("Exit");
        exit.setMnemonic('x');

        staff = new JMenu("Staff");
        staff.setMnemonic('s');

        createFlight = new JMenuItem("New Flight");
        createFlight.setMnemonic('f');
        createFlight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new FlightView().setVisible(true);
            }
        });

        airports = new JMenuItem("Airports");
        airports.setMnemonic('a');
        airports.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainUI.getInstance().getAirportEditor().setVisible(true);
            }
        });

        roles = new JMenuItem("Roles");
        roles.setMnemonic('r');
        roles.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainUI.getInstance().getRoleEditor().setVisible(true);
            }
        });

        users = new JMenuItem("Users");
        users.setMnemonic('u');
        users.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainUI.getInstance().getUserEditor().setVisible(true);
            }
        });

        activeUsers = new JMenuItem("Active Users");
        activeUsers.setMnemonic('a');
        activeUsers.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainUI.getInstance().getActiveUserViewer().setVisible(true);
            }
        });

        tax = new JMenuItem("Manage GST");
        tax.setMnemonic('g');
        tax.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new GSTView().setVisible(true);
            }
        });

        promotions = new JMenuItem("Manage Promotions");
        promotions.setMnemonic('p');
        promotions.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new PricingStrategyListView().setVisible(true);
            }
        });



        help = new JMenu("Help");
        help.setMnemonic('h');

        about = new JMenuItem("About");
        about.setMnemonic('a');

        rebuildMenu();

    }

    void update() {
        rebuildMenu();
    }
}

